package com.yunshan.order;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.yunshan.common.utils.StringUtils;
import com.yunshan.member.domain.MemberUserInfo;
import com.yunshan.member.service.IMemberUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunshan.common.annotation.Log;
import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.enums.BusinessType;
import com.yunshan.order.domain.OrderRechargeRecord;
import com.yunshan.order.service.IOrderRechargeRecordService;
import com.yunshan.common.utils.poi.ExcelUtil;
import com.yunshan.common.core.page.TableDataInfo;

/**
 * 充值订单Controller
 * 
 * @author yunshan
 * @date 2025-01-20
 */
@Api(tags = "充值订单管理")
@RestController
@RequestMapping("/order/recharge")
public class OrderRechargeRecordController extends BaseController
{
    @Autowired
    private IOrderRechargeRecordService orderRechargeRecordService;
    @Autowired
    private IMemberUserInfoService memberUserInfoService;

    /**
     * 查询充值订单列表
     */
    @ApiOperation("查询充值订单列表")
    @PreAuthorize("@ss.hasPermi('order:recharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderRechargeRecord orderRechargeRecord)
    {
        startPage();
        List<OrderRechargeRecord> list = orderRechargeRecordService.selectOrderRechargeRecordList(orderRechargeRecord);
        return getDataTable(list);
    }

    /**
     * 导出充值订单列表
     */
    @ApiOperation("导出充值订单列表")
    @PreAuthorize("@ss.hasPermi('order:recharge:export')")
    @Log(title = "充值订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderRechargeRecord orderRechargeRecord)
    {
        List<OrderRechargeRecord> list = orderRechargeRecordService.selectOrderRechargeRecordList(orderRechargeRecord);
        ExcelUtil<OrderRechargeRecord> util = new ExcelUtil<OrderRechargeRecord>(OrderRechargeRecord.class);
        util.exportExcel(response, list, "充值订单数据");
    }

    /**
     * 获取充值订单详细信息
     */
    @ApiOperation("获取充值订单详细信息")
    @PreAuthorize("@ss.hasPermi('order:recharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderRechargeRecordService.selectOrderRechargeRecordById(id));
    }

    /**
     * 新增充值订单
     */
    @ApiOperation("新增充值订单")
    @PreAuthorize("@ss.hasPermi('order:recharge:add')")
    @Log(title = "充值订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderRechargeRecord orderRechargeRecord)
    {
        if (orderRechargeRecord.getAmount() == null || StringUtils.isEmpty(orderRechargeRecord.getPhone())){
            return AjaxResult.error("请先将内容填写完整~");
        }else if(orderRechargeRecord.getAmount().compareTo(new BigDecimal(0)) <= 0){
            return AjaxResult.error("请输入正整数金额");
        }
        MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(orderRechargeRecord.getMemberId());
        if (memberUserInfo == null){
            return AjaxResult.error("副卡不允许充值或者核实用户信息");
        }else if ("4".equals(memberUserInfo.getMemberType())){
            return AjaxResult.error("临时会籍暂不支持充值");
        }
        return success(orderRechargeRecordService.insertOrderRechargeRecord(orderRechargeRecord));
    }

    /**
     * 修改充值订单
     */
    @ApiOperation("修改充值订单")
    @PreAuthorize("@ss.hasPermi('order:recharge:edit')")
    @Log(title = "充值订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderRechargeRecord orderRechargeRecord)
    {
        return toAjax(orderRechargeRecordService.updateOrderRechargeRecord(orderRechargeRecord));
    }

    /**
     * 删除充值订单
     */
    @ApiOperation("删除充值订单")
    @PreAuthorize("@ss.hasPermi('order:recharge:remove')")
    @Log(title = "充值订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderRechargeRecordService.deleteOrderRechargeRecordByIds(ids));
    }

    /**
     * 审核
     */
    @ApiOperation("审核")
    @PreAuthorize("@ss.hasPermi('order:recharge:edit')")
    @Log(title = "审核", businessType = BusinessType.UPDATE)
    @PostMapping("/process")
    public AjaxResult process(@RequestBody OrderRechargeRecord orderRechargeRecord)
    {
        return toAjax(orderRechargeRecordService.process(orderRechargeRecord));
    }
}
